package mini.reanimate;

import java.util.ArrayList;

import audio.Audio;
import owg.util.data.Pair;
import owg.util.euclidian.Compass;
import owg.util.opengl.ColorF;
import world.Sound;
import hubol.Animation;
import hubol.Shake;
import hubol.Sprite;
import hubol.Subimage;

public class Bogus {
	private Animation aStand, aDance, aJump, aWalk, aFlame;
	private Reanimate mom;
	
	private final double SPEED = 4;
	
	private double goalY = 400;
	private final double leftX = 470, rightX = 235;
	
	private float scale = .4f;
	public float xscale = 1;
	private double x = leftX, y = goalY;
	private double hspeed = 0, vspeed = 0;
	
	private final Pair<Sprite, Animation> stand, dance, jump, walk, flame, flameDance;
	private Pair<Sprite, Animation> current;
	
	private Shake s = new Shake(), b = new Shake();
	private boolean visible = true;
	
	private Sprite shadow = new Sprite("bogusshadow");
	
	private boolean goRight, goLeft;
	private int goCounter;
	private int danceTimer, danceCounter;
	
	private float storedXscale = 1;
	
	public boolean toCancel = true;
	private final float[] shit = new float[]{.15f, .1f, .07f, 0, .07f, .1f, .15f};
	
	public Bogus(Reanimate mother){
		aStand = new Animation(new Subimage(0, 100));
		
		aFlame = new Animation(new Subimage(0, 100, -1));
		
		aDance = new Animation(new Animation(new Subimage(0, .3), new Subimage(1, .3), new Subimage(2, .3)), new Animation(new Subimage(3, .3, 1), new Subimage(4, .3), new Subimage(5, .3), new Subimage(6, .4), new Subimage(5, .3), new Subimage(4, .3), new Subimage(5, .3), new Subimage(6, .4), new Subimage(7, 0), new Subimage(3, .3, -1), new Subimage(4, .3), new Subimage(5, .3), new Subimage(6, .4), new Subimage(5, .3), new Subimage(4, .3), new Subimage(5, .3), new Subimage(6, .4), new Subimage(7, 0)));
		aDance.setSpeed(1.6f);
		
		ArrayList<Subimage> temp = new ArrayList<Subimage>();
		for (int i=1; i<8; i++)
			temp.add(new Subimage(i, .3 + shit[i - 1]));
		aWalk = new Animation(temp);
		aWalk.setSpeed(1.5f);
		
		ArrayList<Subimage> uemp = new ArrayList<Subimage>();
		for (int i=0; i<6; i++)
			uemp.add(new Subimage(i, .5));
		uemp.add(new Subimage(6, 1.5));
		int i = 5; //lol
		while(i > -1){
			uemp.add(new Subimage(i, .5));
			i--;
		}
		aJump = new Animation(uemp);
		aJump.setLooping(false);
		aJump.setSpeed(7.2f);
		
		mom = mother;
		
		stand = new Pair<Sprite, Animation>(new Sprite("walk", Compass.SOUTH), aStand);
		dance = new Pair<Sprite, Animation>(new Sprite("randydance", Compass.SOUTH), aDance);
		flameDance = new Pair<Sprite, Animation>(new Sprite("randydanceflame", Compass.SOUTH), aDance);
		jump = new Pair<Sprite, Animation>(new Sprite("jump", Compass.SOUTH), aJump);
		walk = new Pair<Sprite, Animation>(new Sprite("walk", Compass.SOUTH), aWalk);
		flame = new Pair<Sprite, Animation>(new Sprite("flamethrow", Compass.SOUTH), aFlame);
		current = stand;
	}
	
	public void step(){
		x += hspeed;
		y += vspeed;
		
		if (y < goalY){
			vspeed += .9;
		}
		else{
			vspeed = 0;
			if (y != goalY){
				Sound.playPitched("jumpland");
				y = goalY;
				
				mom.bogusLand();
				complete();
			}
		}
		
		if (goRight || goLeft){
			goCounter++;
			if (goCounter % 45 == 0)
				Sound.playPitched("step");
		}
		
		if (goRight){
			if (mom.level == mom.LEFT){
				if (x >= 864){
					mom.setLevel(mom.RIGHT);
					x -= 864;
				}
			}
			else{
				if (x >= rightX){
					x = rightX;
					hspeed = 0;
					Sound.playPitched("step");
					goRight = false;
					complete();
				}
			}
		}
		else if (goLeft){
			if (mom.level == mom.RIGHT){
				if (x <= 0){
					mom.setLevel(mom.LEFT);
					x += 864;
				}
			}
			else{
				if (x <= leftX){
					x = leftX;
					hspeed = 0;
					Sound.playPitched("step");
					goLeft = false;
					complete();
				}
			}
		}
		
		if (danceTimer > 0){
			danceTimer--;
			if (danceCounter % 30 == 0)
				Sound.playPitched("randydance");
			danceCounter++;
			if (danceTimer == 0)
				complete();
		}
		
		current.b.step();
	}
	
	public void setPosition(double x, double y){
		this.x = x;
		this.y = y;
		goalY = y;
	}
	
	public void visible(){
		visible = true;
	}
	
	public void invisible(){
		visible = false;
	}
	
	public void left(){
		goLeft = true;
		hspeed = -SPEED;
		xscale = -1;
		walk();
	}
	
	public void right(){
		goRight = true;
		hspeed = SPEED;
		xscale = 1;
		walk();
	}
	
	public void jump(){
		storedXscale = xscale;
		danceTimer = 0;
		Sound.playPitched("jump");
		Audio.getClip("randydance").stop();
		vspeed = -14;
		anim(jump);
	}
	
	public void dance(int length){
		danceTimer = length;
		anim(dance);
	}
	
	public void flameDance(int length){
		danceTimer = length;
		anim(flameDance);
	}
	
	public void walk(){
		Sound.playPitched("step");
		goCounter = 0;
		anim(walk);
	}
	
	public void flame(){
		anim(flame);
	}
	
	private void anim(Pair<Sprite, Animation> p){
		p.b.reset();
		current = p;
		mom.BUSY = true;
	}
	
	public void setScale(float scale){
		this.scale = scale;
	}
	
	public void render(boolean drawShadow){
		if (visible){
			if (drawShadow)
				shadow.render(0, x + b.x, goalY + b.y, scale, scale, 0);
			double addY = 0;
			int img = current.b.getSubimage();
			if (current == walk){
				addY = 8 + (shit[img - 1] * -25);
			}
			current.a.render(img, x + s.x, y + s.y + addY, xscale * current.b.getXscale() * scale, scale, 0, ColorF.WHITE);
		}
	}
	
	public void complete(){
		current = stand;
		xscale = storedXscale;
		
		if (toCancel)
			mom.BUSY = false;
	}
	
	

}
